import React from 'react';
import { Tabs } from 'antd';
import './index.less'

const { TabPane } = Tabs;

class TabList extends React.Component {
    constructor(props) {
        super(props);
        this.newTabIndex = 0;
        const panes = [
            { 
                title: '用户中心',
                path:'/index/userCenter',
                key: '用户中心',
                closable: false,
            }
        ];
        this.state = {
            activeKey:panes[0].key,
            panes:panes,
        };
    }
    componentWillReceiveProps(nextProps) {
        let { activeKey } = this.state
        let { panes } = this.state
        let isRepeat = false
        panes.forEach((item)=>{
            if(item.key === nextProps.selectName[0]){
                isRepeat = true
            }
        })
        if(isRepeat === false){
            panes.push({ 
                title: nextProps.selectName[0], 
                content: nextProps.selectName[0], 
                path: nextProps.selectPath, 
                key: nextProps.selectName[0] 
            });
        }
        activeKey = nextProps.selectName[0]
        this.setState({ panes,activeKey })
    }
    onChange = activeKey => {
        let { panes } = this.state
        for(let item of panes){
            if(item.key ===activeKey){
                this.setState({ activeKey });
                this.props.onTabSelect(activeKey,item.path)
                break
            }
        }
        
    };
    onEdit = (targetKey, action) => {
        this[action](targetKey);
    };


    remove = targetKey => {
        let { activeKey } = this.state;
        let lastIndex;
        let pathValue = '';
        this.state.panes.forEach((pane, i) => {
          if (pane.key === targetKey) {
            lastIndex = i - 1;
          }
        });
        let panes = this.state.panes.filter(pane => pane.key !== targetKey);
        if (panes.length && activeKey === targetKey) {
            if (lastIndex >= 0) {
                activeKey = panes[lastIndex].key;
                pathValue = panes[lastIndex].path;
            } else {
                activeKey = panes[0].key;
                pathValue = panes[0].path;
            }
            this.setState({ panes,activeKey},()=>{
                this.props.onTabSelect(activeKey,pathValue)
            });
        }
        else{
            this.setState({ panes,activeKey});
        }   
        
    };

    render() {
        return (
        <Tabs
            hideAdd
            onChange={this.onChange}
            activeKey={this.state.activeKey}
            type="editable-card"
            onEdit={this.onEdit}
        >
            {this.state.panes.map(pane => (
                <TabPane tab={pane.title} key={pane.key} closable={pane.closable}> 

                </TabPane>
            ))}
        </Tabs>
        );
    }
}

export { TabList }